package com.india.project.push.server;

import com.india.common.utils.bean.ApplicationContextProvider;
import com.india.project.push.cachedata.CacheData;
import com.india.project.push.domain.SoHvBarcodeVo;
import com.india.project.push.domain.WmsToOtcDnLog;
import com.india.project.push.mapper.WmsToOtcDnLogMapper;
import com.india.project.push.otcwms.DealerForHilwmsClient;
import com.india.project.push.otcwms.DealerForHilwmsClientPortType;
import com.india.project.push.otcwms.StoDtlCheckInfoDTO;
import com.india.project.push.service.impl.SoHvBarCodeVoImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class WmsToOtcConsumerServer implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(WmsToOtcConsumerServer.class);
    private boolean runFlag;
    private int threadNum;

    public void setRunFlag(boolean runFlag) {
        this.runFlag = runFlag;
    }

    public void setThreadNum(int threadNum) {
        this.threadNum = threadNum;
    }

    @Override
    public void run() {
        while (runFlag) {
            log.info("****************************** DealerForHilWmsClientQuartz dataProcess() Started To Run ******************************");
            try {
                SoHvBarCodeVoImpl soHvBarCodeVoImpl = ApplicationContextProvider.getBean(SoHvBarCodeVoImpl.class);
                WmsToOtcDnLogMapper wmsToOtcDnLogMapper = ApplicationContextProvider.getBean(WmsToOtcDnLogMapper.class);
                WmsToOtcDnLog wmsToOtcDnLog = new WmsToOtcDnLog();
                DealerForHilwmsClient client = new DealerForHilwmsClient();
                DealerForHilwmsClientPortType port = client.getDealerForHilwmsClientPort();
                List<SoHvBarcodeVo> barcodeVoList = CacheData.wms_msg_que.take();
                if (barcodeVoList.size() > 0) {
                    ArrayList<StoDtlCheckInfoDTO> list = new ArrayList<>();
                    log.info("查询结束，开始STO推送......");
                    StoDtlCheckInfoDTO info = new StoDtlCheckInfoDTO();
                    info.setAdded1("PUNE");
                    info.setOrderNo(barcodeVoList.get(0).getSo());// SO
                    info.setDn(barcodeVoList.get(0).getDnNo());
                    info.setMatCode(barcodeVoList.get(0).getMatCode());
                    // 行项目
                    info.setItemNo(barcodeVoList.get(0).getItemNo());
                    info.setBarcode(barcodeVoList.get(0).getSern());
                    info.setStatus("0");
                    list.add(info);
                    wmsToOtcDnLog.setSo(barcodeVoList.get(0).getSo());
                    wmsToOtcDnLog.setDnNo(barcodeVoList.get(0).getDnNo());
                    wmsToOtcDnLog.setMatCode(barcodeVoList.get(0).getMatCode());
                    wmsToOtcDnLog.setItemNo(barcodeVoList.get(0).getItemNo());
                    wmsToOtcDnLog.setSern(barcodeVoList.get(0).getSern());
                    wmsToOtcDnLog.setOgpNo(barcodeVoList.get(0).getOgpno());
                    wmsToOtcDnLog.setOrderType(barcodeVoList.get(0).getOrderType());
                    wmsToOtcDnLog.setCreatedBy("WmsToOtcQuartz");
                    List<String> barcodeList = list.stream().map(StoDtlCheckInfoDTO::getBarcode).collect(Collectors.toList());
                    String result = port.saveBarcodesFromSto(list);
                    if (result.equals("0")) {
                        soHvBarCodeVoImpl.otcWmsSuccess(barcodeList);
                        wmsToOtcDnLog.setPushStatus("Y");
                    } else {
                        soHvBarCodeVoImpl.otcWmsFail(barcodeList);
                        wmsToOtcDnLog.setPushStatus("N");
                        log.info(" 失败条数" + result + " dataProcess end ");
                    }
                    wmsToOtcDnLogMapper.saveLog(wmsToOtcDnLog);
                }

            } catch (Exception ex) {
                ex.printStackTrace();
                // 捕获异常信息
                log.info(ex.getMessage());
            } finally {
                log.info("====================================== 此次任务调用结束 ======================================");
            }
            log.info("****************************** DealerForHilWmsClientQuartz dataProcess() end  ***********************************");
        }
    }
}
